﻿(function ($) {
    $.personjs = {
        initCustomerInfo: function (CustomerName) {

            $("#CustomerName").empty();
            var optionstring = "";
            $.ajax({
                url: "../RoleInfo/RoleInfo.ashx",
                data: {
                    Action: "initCustomerInfo",
                    CustomerName: CustomerName
                },
                async: true,
                type: "post",
                datatype: "json",
                success: function (data) {
                    Loading(false);
                    data = JSON.parse(data);
                    console.log(data);
                    if (data.length > 0) {
                        for (i in data) {
                            optionstring += "<option value=\"" + data[i].ID + "\" >" + data[i].CustomName.trim() + "</option>";
                        }
                        if (CustomerName == "宏大") {
                            $("#CustomerName").html("<option value=''>请选择</option>" + "<option value='0'>宏大</option>" + optionstring);
                        }
                        else {
                            $("#CustomerName").html("<option value=''>请选择</option>"  + optionstring);
                        }  
                    }
                    else
                        dialogMsg(data.msg, -1);
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    Loading(false);
                    dialogMsg(errorThrown, -1);
                }
            });
        },

        initRoleInfo: function (customename) {

            $("#RoleName").empty();
            var optionstring = "";
            $.ajax({
                url: "./PersonInfo.ashx",
                data: {
                    Action: "initRoleInfo",
                    CustomeName: customename
                },
                async: true,
                type: "post",
                datatype: "json",
                success: function (data) {
                    Loading(false);
                    data = JSON.parse(data);
                    console.log(data);
                    if (data.length > 0) {
                        for (i in data) {
                            optionstring += "<option value=\"" + data[i].ID + "\" >" + data[i].RoleName.trim() + "</option>";
                        }
                        $("#RoleName").html("<option value=''>请选择</option>"  + optionstring);
                    }
                    else
                        $("#RoleName").html("<option value=''>请选择</option>");
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    Loading(false);
                    dialogMsg(errorThrown, -1);
                }
            });
        },

        savePersonInfo: function (UserNo, UserName, Password, Tel, CustomerName, RoleName,grid) {

            $.ajax({
                url: "./PersonInfo.ashx",
                data: {
                    Action: "addPersonInfo",
                    UserNo: UserNo,
                    UserName: UserName,
                    Password: Password,
                    Tel: Tel,
                    CustomerName: CustomerName,
                    RoleName: RoleName
                },
                type: "post",
                async: true,
                datatype: "json",
                success: function (data) {
                    if (data == "success") {
                        Loading(false);
                        dialogMsg("保存成功", 1);
                        grid.trigger("reloadGrid");
                        dialogClose();
                        
                    }
                    else if (data == "falut") {
                        dialogMsg("保存失败", -1);
                    }
                    else if (data == "hasexit") {
                        dialogMsg("已存在此用户", 0);
                    }

                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    Loading(false);
                    dialogMsg(errorThrown, -1);
                },
                beforeSend: function () {
                    Loading(true, "正在保存数据");
                },
                complete: function () {
                    Loading(false);
                }
            });
        },

        //保存编辑 用户
        editPersonInfo: function (UserNo, UserName, Password, Tel, CustomerName, RoleName, grid) {
            $.ajax({
                url: "./PersonInfo.ashx",
                data: {
                    Action: "editPersonInfo",
                    UserNo: UserNo,
                    UserName: UserName,
                    Password: Password,
                    Tel: Tel,
                    CustomerName: CustomerName,
                    RoleName: RoleName
                },
                type: "post",
                async: true,
                datatype: "json",
                success: function (data) {
                    if (data == "success") {
                        Loading(false);
                        dialogMsg("保存成功", 1);
                        grid.trigger("reloadGrid");
                        dialogClose();
                        
                    }
                    else if (data == "falut") {
                        dialogMsg("保存失败", -1);
                    }
                  
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    Loading(false);
                    dialogMsg(errorThrown, -1);
                },
                beforeSend: function () {
                    Loading(true, "正在保存数据");
                },
                complete: function () {
                    Loading(false);
                }
            });
        },

        deletePersonInfo: function (UserID,grid) {
            $.ajax({
                url: "./PersonInfo.ashx",
                data: {
                    Action: "deletePersonInfo",
                    UserID: UserID
                },
                type: "post",
                async: true,
                datatype: "json",
                success: function (data) {
                    console.log(data);
                    if (data == "success") {
                        Loading(false);
                        dialogMsg("删除成功", 1);
                        grid.trigger("reloadGrid");
                        dialogClose();
                    }
                    else if (data == "falut") {
                        dialogMsg("删除失败", -1);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    Loading(false);
                    dialogMsg(errorThrown, -1);
                },
                beforeSend: function () {
                    Loading(true, "正在保存数据");
                },
                complete: function () {
                    Loading(false);
                }
            });
        },

        //编辑用户初始化界面
        initeditperson: function (PersonID) {
            $.ajax({
                url: "./PersonInfo.ashx",
                data: {
                    Action: "initeditperson",
                    PersonID: PersonID
                },
                async: true,
                type: "post",
                datatype: "json",
                success: function (data) {
                    Loading(false);
                    data = JSON.parse(data);
                    console.log(data);
                    if (data.length > 0) {
                        $("#UserNo").val(data[0].UserNo);
                        $("#UserName").val(data[0].UserName);
                        $("#UserPwd").val(data[0].UserPwd);
                        $("#Tel").val(data[0].Tel);
                        $("#RoleName").val(data[0].RoleName);
                        $("#CustomerName").val(data[0].CustomerName);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    Loading(false);
                    dialogMsg(errorThrown, -1);
                }
            });
        }
    };


    $(function () {

    });
})(jQuery);